package com.mstanford.movie.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mstanford.movie.dao.MovieDao;
import com.mstanford.movie.utils.DataUtil;

public class QueryTimeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String date = request.getParameter("date");
		String edition = request.getParameter("edition");
		String play = request.getParameter("play");
		String time = request.getParameter("time");
		
		Map<String, String> argMap = new HashMap<String, String>();
		argMap.put("date", date);
		argMap.put("edition", edition);
		argMap.put("play", play);
		
		MovieDao dao = new MovieDao();
		List<Map<String, String>> list = dao.queryPlayTime(argMap);

		if(!DataUtil.isEmpty(time)) {
			removeDataByTime(list, time);
		}
		
		Map<String, List<String>> movieMap = buildResultData(list);
		
		String movieJson = DataUtil.map2Json(movieMap);
		String playJson = DataUtil.listToJson(list);

		String json = "{\"movie\":" + movieJson + ",\"play\":" + playJson + "}";
		
		// 返回Json格式信息
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		out.print(json);
		out.flush();
		out.close();
	}
	
	/**
	 * 根据上午,下午,晚上,全部条件过滤数据
	 * @param list
	 * @param type
	 */
	private void removeDataByTime(List<Map<String, String>> list, String type) {
		
		String pattern = "yyyy-MM-dd HH:mm";
		
		for(int i=0; i<list.size(); i++) {
			Map<String, String> map = list.get(i);
			String playTime1 = map.get("playTime1");
			String playTime2 = map.get("playTime2");
			String playTime = playTime1 + " " + playTime2;
			
			boolean flag = false;
			
			if("ampm".equals(type)) { // 上午下午(排除晚上)
				String dateTime1 = playTime1 + " 18:00";
				String dateTime2 = playTime1 + " 23:59";
				
				if(DataUtil.compareDate(playTime, dateTime1, pattern) == 1 && 
						DataUtil.compareDate(playTime, dateTime2, pattern) != 1) {
					flag = true;
				}
			} else if("amnight".equals(type)) { // 上午晚上(排除下午)
				String dateTime1 = playTime1 + " 12:00";
				String dateTime2 = playTime1 + " 18:00";
				
				if(DataUtil.compareDate(playTime, dateTime1, pattern) == 1 && 
						DataUtil.compareDate(playTime, dateTime2, pattern) != 1) {
					flag = true;
				}
			} else if("pmnight".equals(type)) { // 下午晚上(排除上午)
				String dateTime1 = playTime1 + " 00:00";
				String dateTime2 = playTime1 + " 12:00";
				
				if(DataUtil.compareDate(playTime, dateTime1, pattern) != -1 && 
						DataUtil.compareDate(playTime, dateTime2, pattern) != 1) {
					flag = true;
				}
			} else if("am".equals(type)) { // 上午(排除下午和晚上)
				String dateTime1 = playTime1 + " 12:00";
				String dateTime2 = playTime1 + " 18:00";
				String dateTime3 = playTime1 + " 18:00";
				String dateTime4 = playTime1 + " 23:59";
				
				if((DataUtil.compareDate(playTime, dateTime1, pattern) == 1 && 
						DataUtil.compareDate(playTime, dateTime2, pattern) != 1) || 
						(DataUtil.compareDate(playTime, dateTime3, pattern) == 1 && 
								DataUtil.compareDate(playTime, dateTime4, pattern) != 1)) {
					flag = true;
				}
			} else if("pm".equals(type)) { // 下午(排除上午和晚上)
				String dateTime1 = playTime1 + " 00:00";
				String dateTime2 = playTime1 + " 12:00";
				String dateTime3 = playTime1 + " 18:00";
				String dateTime4 = playTime1 + " 23:59";
				
				if((DataUtil.compareDate(playTime, dateTime1, pattern) != -1 && 
						DataUtil.compareDate(playTime, dateTime2, pattern) != 1) || 
						(DataUtil.compareDate(playTime, dateTime3, pattern) == 1 && 
								DataUtil.compareDate(playTime, dateTime4, pattern) != 1)) {
					flag = true;
				}
			} else if("night".equals(type)) { // 晚上(排除上午和下午)
				String dateTime1 = playTime1 + " 00:00";
				String dateTime2 = playTime1 + " 12:00";
				String dateTime3 = playTime1 + " 12:00";
				String dateTime4 = playTime1 + " 18:00";
				
				if((DataUtil.compareDate(playTime, dateTime1, pattern) != -1 && 
						DataUtil.compareDate(playTime, dateTime2, pattern) != 1) || 
						(DataUtil.compareDate(playTime, dateTime3, pattern) == 1 && 
								DataUtil.compareDate(playTime, dateTime4, pattern) != 1)) {
					flag = true;
				}
			}
			
			if(flag) {
				list.remove(i);
				i --;
			}
		}
	}
	
	private Map<String, List<String>> buildResultData(List<Map<String, String>> list) {
		
		Map<String, List<String>> movieMap = new HashMap<String, List<String>>();
		
		for(Map<String, String> map : list) {
			String playId = map.get("playId");
			String movieName = map.get("movieName");
			
			List<String> movieList = movieMap.get(movieName);
			
			if(movieList == null) {
				movieList = new ArrayList<String>();
				movieMap.put(movieName, movieList);
			}
			
			movieList.add(playId);
		}
		
		String preMovieName = "";
		
		for(Map<String, String> map : list) {
			String movieName = map.get("movieName");
			List<String> movieList = movieMap.get(movieName);
			int rowspan = movieList.size() == 0 ? 1 : movieList.size();
			
			if(!preMovieName.equals(movieName)) {
				preMovieName = movieName;
				
				if(rowspan > 1) {
					map.put("rowspan", rowspan + "");
				}
			}
		}
		
		return movieMap;
	}
}